package com.example.zhu95.mytimber;

import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.support.v4.content.LocalBroadcastManager;

import org.litepal.LitePal;

import static com.example.zhu95.mytimber.ConstantValue.CURRENT_POSITION;
import static com.example.zhu95.mytimber.ConstantValue.IS_PROGRESSING;
import static com.example.zhu95.mytimber.ConstantValue.MUSIC_STATE;
import static com.example.zhu95.mytimber.ConstantValue.MUSIC_STATE_RECEIVE;
import static com.example.zhu95.mytimber.ConstantValue.PlayMusicCmd;
import static com.example.zhu95.mytimber.ConstantValue.SONG_ID;
import static com.example.zhu95.mytimber.ConstantValue.SONG_IDS;
import static com.example.zhu95.mytimber.ConstantValue.START_PLAY_RECEIVE;

/**
 * @author zhu95 on 2017/11/29.
 */

public class MyApplication extends Application {
    public static Context context;

    public static Context getContext() {
        return context;
    }

    public static LocalBroadcastManager localBroadcastManager;

    public static LocalBroadcastManager getLocalBroadcastManager() {
        localBroadcastManager = LocalBroadcastManager.getInstance(getContext());
        return localBroadcastManager;
    }

    /**
     * 发送广播以通知UI变化
     *
     * @param songid
     * @param musicState
     * @param currentPosition
     * @param isProgressing
     */
    public static void sendMusicStateBroadcast(int songid, String musicState, int currentPosition, boolean isProgressing) {
        Intent intent = new Intent(MUSIC_STATE_RECEIVE);
        intent.putExtra(SONG_ID, songid);
        intent.putExtra(MUSIC_STATE, musicState);
        intent.putExtra(CURRENT_POSITION, currentPosition);
        //表示是否是专门为了更新progress而发送的广播
        intent.putExtra(IS_PROGRESSING, isProgressing);
        MyApplication.getLocalBroadcastManager().sendBroadcast(intent);
    }

    /**
     * 发送广播以播放歌曲
     *
     * @param songid
     * @param songids
     * @param cmd
     */
    public static void sendPlayMusicCmdBroadcast(int songid, String songids, String cmd) {
        sendPlayMusicCmdBroadcast(songid, songids, cmd, -1);
    }

    public static void sendPlayMusicCmdBroadcast(int songid, String songids, String cmd, int currentPosition) {
        Intent intent = new Intent(START_PLAY_RECEIVE);
        intent.putExtra(SONG_ID, songid);
        intent.putExtra(SONG_IDS, songids);
        intent.putExtra(PlayMusicCmd, cmd);
        intent.putExtra(CURRENT_POSITION, currentPosition);
        MyApplication.getLocalBroadcastManager().sendBroadcast(intent);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        context = getApplicationContext();
        LitePal.initialize(context);
        LitePal.getDatabase();
    }
}
